package com.yipinhouse.signing.service.consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yipinhouse.common.enums.HouseTypeEnum;
import com.yipinhouse.house.client.NewHouseQueryClient;
import com.yipinhouse.house.client.ResoldApartmentManageClient;
import com.yipinhouse.house.client.ResoldHouseManageClient;
import com.yipinhouse.signing.common.IntentionMoneyStatusEnum;
import com.yipinhouse.signing.infra.model.IntentionMoney;
import com.yipinhouse.signing.service.IntentionMoneyService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
@RocketMQMessageListener(topic = "yipinhouse-intentionMoney-cancel",consumerGroup = "yipinhouse-intentionMoney-cancel")
public class CancelIntentionMoneyConsumer implements RocketMQListener<String> {

    @Resource
    private ResoldApartmentManageClient resoldApartmentManageClient;
    @Resource
    private ResoldHouseManageClient resoldHouseManageClient;
    @Resource
    private NewHouseQueryClient newHouseQueryClient;
    @Resource
    private IntentionMoneyService intentionMoneyService;
    @Override
    public void onMessage(String message) {
        System.out.println("执行订单回查");
        IntentionMoney intentionMoney = intentionMoneyService.getOne(new QueryWrapper<IntentionMoney>()
                .eq("intention_money_num",message));
        if (intentionMoney.getStatus().equals(IntentionMoneyStatusEnum.TO_BE_PAID.getCode())){
            //到时未付款
            intentionMoney.setStatus(IntentionMoneyStatusEnum.INVALID.getCode());
            intentionMoneyService.updateById(intentionMoney);
            //2.修改房源状态为可售
            if (intentionMoney.getHouseType().equals(HouseTypeEnum.NEW_HOUSE.getCode())){
                //新房
                newHouseQueryClient.onSealHouse(intentionMoney.getHouseId());
            }else if (intentionMoney.getHouseType().equals(HouseTypeEnum.SECOND_HAND_HOUSING.getCode())){
                //二手房
                resoldApartmentManageClient.onSaleHouse(intentionMoney.getHouseId());
            }else if (intentionMoney.getHouseType().equals(HouseTypeEnum.RENTAL_HOUSING.getCode())){
                //出租房
                resoldHouseManageClient.onSaleHouse(intentionMoney.getHouseId());
            }
        }

    }
}
